package com.szly.phm.common.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.szly.phm.common.entity.dto.parameter.CreateParameterTypeDTO;
import com.szly.phm.common.entity.dto.parameter.ParameterTypeDTO;
import com.szly.phm.common.entity.dto.parameter.UpdateParameterTypeDTO;
import com.szly.phm.common.entity.dto.user.LoginUserDTO;
import com.szly.phm.common.entity.po.SysParameterType;
import com.szly.phm.common.entity.result.CodeStatusEnum;
import com.szly.phm.common.entity.result.PageResult;
import com.szly.phm.common.entity.result.RootResponse;
import com.szly.phm.common.entity.vo.parameter.ParameterTypeVO;
import com.szly.phm.common.mapper.SysParameterTypeMapper;
import com.szly.phm.common.service.ISysParameterTypeService;
import com.szly.phm.common.utils.BeanCopyUtils;
import com.szly.phm.common.utils.HandleUtils;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;

/**
 * phm
 *
 * @author: git.jas0nch
 * date: 2021-10-10
 */
@Service
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
public class SysParameterTypeServiceImpl extends ServiceImpl<SysParameterTypeMapper, SysParameterType> implements ISysParameterTypeService {

    // query

    /**
     *
     * @param parameterTypeDTO all the infomation here;
     * @return
     */
    @Override
    public RootResponse<PageResult<ParameterTypeVO>> queryParameterTypePage(ParameterTypeDTO parameterTypeDTO) {
        var wrapper = new LambdaQueryWrapper<SysParameterType>();
        //
        if(parameterTypeDTO.getCodeName()!=null){
            wrapper.like(SysParameterType::getCodeName,parameterTypeDTO.getCodeName());
        }
        wrapper.eq(SysParameterType::getIsSoftDelete, false);
        wrapper.orderByDesc(SysParameterType::getCreateDateTime);
        //
        wrapper.orderByDesc(SysParameterType::getCreateDateTime, SysParameterType::getId);
//        if ("00".equals(permissionDTO.getType())) {
        // page helper
        PageHelper.startPage(parameterTypeDTO.getPageNum(), parameterTypeDTO.getPageSize());
        var parameterType = baseMapper.selectList(wrapper);
        var pageInfo = PageInfo.of(parameterType);
        var pageList = pageInfo.getList();
        return RootResponse.success(PageResult.page(pageList, pageInfo.getPageNum(), pageInfo.getPageSize(), pageInfo.getTotal()));
//        } else {
//            var permissions = baseMapper.selectList(wrapper);
//            return RootResponse.success(permissions);
//        }
    }

    @Override
    public RootResponse createParameterType(CreateParameterTypeDTO createParameterTypeDTO, LoginUserDTO userDTO) {
        //检查重名
        var wrapper = new LambdaQueryWrapper<SysParameterType>();
        wrapper.eq(SysParameterType::getCodeName, createParameterTypeDTO.getCodeName());
        wrapper.eq(SysParameterType::getIsSoftDelete, false);
        var parameterType = baseMapper.selectOne(wrapper);
        if (parameterType != null) {
            return RootResponse.error( "路由已存在，请勿重复添加");
        }
        parameterType = new SysParameterType();
//        BeanCopyUtils.copy(src,dest);
        BeanCopyUtils.copy(createParameterTypeDTO, parameterType);
        parameterType.setId(HandleUtils.UUID32());
        parameterType.setCreator(userDTO.getUsername());
        parameterType.setCreateDateTime(new Date());
        parameterType.setIsSoftDelete(false);
//        if (StringUtils.isNotBlank(createParameterTypeDTO.getPermissionPid())) {
//            var parent = baseMapper.selectById(permissionDTO.getPermissionPid());
//            permission.setIdPath(parent.getIdPath() + "/" + permission.getId());
//        } else {
//            permission.setIdPath(permission.getId());
//        }
        int i = baseMapper.insert(parameterType);
        return i == 1 ? RootResponse.success(parameterType) : RootResponse.error();
    }

    @Override
    public RootResponse updateParameterType(UpdateParameterTypeDTO updateParameterTypeDTO, LoginUserDTO userDTO) {
        var wrapper1 = new LambdaQueryWrapper<SysParameterType>();
        wrapper1.eq(SysParameterType::getId, updateParameterTypeDTO.getId());
        wrapper1.eq(SysParameterType::getIsSoftDelete, false);
        var parameterType = baseMapper.selectOne(wrapper1);
        if (parameterType == null) {
            return RootResponse.error(CodeStatusEnum.NOT_EXIST_DATA);
        }
        //检查重名
        if (!updateParameterTypeDTO.getCodeName().equals(parameterType.getCodeName())) {
            wrapper1.clear();
            wrapper1.eq(SysParameterType::getIsSoftDelete, false);
            wrapper1.and(wrapper->wrapper.eq(SysParameterType::getCodeName, updateParameterTypeDTO.getCodeName()));
            var parameterType2 = baseMapper.selectOne(wrapper1);
            if (parameterType2 != null) {
                return RootResponse.error(CodeStatusEnum.EXIST_USERNAME.getCode(), "路由已存在，请勿重复添加");
            }
        }
        BeanCopyUtils.copy(updateParameterTypeDTO, parameterType);
        parameterType.setUpdater(userDTO.getUsername());
        parameterType.setUpdateDateTime(new Date());
//        if (org.apache.commons.lang3.StringUtils.isNotBlank(updateParameterTypeDTO.getPermissionPid())) {
//            if (!permissionDTO.getPermissionPid().equals(permission.getPermissionPid())) {
//                var parent = baseMapper.selectById(permissionDTO.getPermissionPid());
//                permission.setIdPath(parent.getIdPath() + "/" + permission.getId());
//            } else {
//                permission.setIdPath(permission.getId());
//            }
//        }
        int i = baseMapper.updateById(parameterType);
        return i == 1 ? RootResponse.success(parameterType) : RootResponse.error();
    }

    /**
     *
     * @param id : parameterType id
     * @param userDTO
     * @return
     */
    @Override
    public RootResponse deleteParameterType(String id, LoginUserDTO userDTO) {
        var wrapper = new LambdaQueryWrapper<SysParameterType>();
        wrapper.eq(SysParameterType::getId, id);
        wrapper.eq(SysParameterType::getIsSoftDelete, false);
        var parameterType = baseMapper.selectOne(wrapper);
        if (parameterType== null) {
            return RootResponse.error(CodeStatusEnum.NOT_EXIST_DATA);
        }
        parameterType.setIsSoftDelete(true);
        parameterType.setUpdateDateTime(new Date());
        parameterType.setUpdater(userDTO.getUsername());
        baseMapper.updateById(parameterType);
        // 根据权限Id删除角色和权限的关联关系
//        var qWrapper = new LambdaQueryWrapper<SysParameterType>();
//        qWrapper.eq(SysParameterType::getPermissionId, permissionId);
//        rolePermissionMapper.delete(qWrapper);
        return RootResponse.success(parameterType);
    }


}
